<?php
  require_once '../../Helpers/DbHelper.php';
  require_once '../../Controllers/AdminController.php';
  require_once '../../Controllers/UserController.php';
  require_once '../../Helpers/PathHelper.php';
  //
  $u = new UserController();
  $u->evaluatePagePermission(UserController::ADMINISTRATOR);
  //
  $c = new AdminController();
  $yearLevelList = $c->getYearLevel();

  $tid = $_GET['tid'];

  if(isset($_GET['tid']) && empty($_POST))
  {
    $teacherDetails = $c->getTeacherDetails($tid);
    $rows = mysql_fetch_assoc($teacherDetails);
    //
    $yearId = $rows['YearLevelId'];
    $teacherName = $rows['TeachersName'];
  }
  if(isset($_GET['tid']) && !empty($_POST))
  {
    $teacherName = $_POST['teacher-name'];
    $yearId = $_POST['year'];
    $c->updateTeacherDetails($tid, $teacherName, $yearId);
  }
?>
<html>
  <head>
    <title>School Enrollment :: Edit Teacher Details</title>
    <?php
      require_once '../commons.php';
      renderHeaderStyles() ;
    ?>  </head>
    <body>
    <div class="admin-container">
      <?php renderAdminNav(); ?>
    </div>
  <div class="container">
    <h1>Edit Teacher Details</h1>
    <form name="teachers-form" method="post">
      <label for="teacher-name">Teachers Name: </label>
      <input type="text" name="teacher-name" value="<?php echo $teacherName; ?>"/><br />
      <label for="year-level">Choose a year level: </label>
      <select name="year">
        <?php
          if(mysql_num_rows($yearLevelList) < 1)
            echo "<option value='0'>No data available</option>";

          while($rows=mysql_fetch_assoc($yearLevelList))
          {
            $i = $rows['YearLevelId'];
            $n = $rows['YearLevelDescription'];
            $content ="<option value='$i'";
            if($i == $yearId)
              $content.= 'selected';

            $content.=">$n</option>";
            echo $content;
          }
        ?>
      </select><br /><br />
      <input type="submit" name="update-teacher" value="Update Teacher Details" />
    </form>
    <hr />
    <a href="<?php echo PathHelper::getBaseUrl()?>/Views/Admin/manage_teachers.php" id="prev-next">&larr;&nbsp;Back</a>
    </table>
  </div>
  </body>
</html>